package cz.fit.vutbr.pis2012.augeaswh.control.whman.slots;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import cz.fit.vutbr.pis2012.augeaswh.managers.SlotsManager;
import cz.fit.vutbr.pis2012.augeaswh.persistence.Slot;

@ManagedBean
@SessionScoped
public class SlotsEditorBean implements Serializable {
	private static final long serialVersionUID = 2099340398946046041L;
	@EJB
	private SlotsManager sm;
	
	private Slot slot;
	private String name;
	private String sector;
	private BigDecimal capacity;

	public SlotsEditorBean() {
		setSlot(null);
	}
	
	public Slot getSlot() {
		return slot;
	}

	public void setSlot(Slot slot) {
		this.slot = slot;
		name=sector="";
		capacity = new BigDecimal(0);
		if(this.slot!=null) {
			this.name=slot.getName();
			this.sector=slot.getSector();
			this.capacity=slot.getCapacity();
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSector() {
		return sector;
	}

	public void setSector(String sector) {
		this.sector = sector;
	}
	
	public BigDecimal getCapacity() {
		return capacity;
	}

	public void setCapacity(BigDecimal capacity) {
		this.capacity = capacity;
	}

	public String submit() {
		
		if(slot==null) {
			this.slot=new Slot();
		}
		this.slot.setName(name);
		this.slot.setSector(sector);
		this.slot.setCapacity(capacity);
		sm.save(slot);
		setSlot(null);
		return "back";
	}
	
	public String getTitle() {
		if(this.slot==null) {
			return "New slot";
		}
		else {
			return "Edited slot \""+this.slot.getName()+"\"";
		}			
	}
}
